package com.yhy.server.service;

import com.alibaba.fastjson.JSON;
import com.yhy.domain.mongo.Publish;
import com.yhy.dubbo.api.mongo.PublishApi;
import com.yhy.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class MovementsMQService {

    @Reference
    private PublishApi publishApi;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 发送MQ消息
     *
     * 需要参数: 1.publishId (动态id)
     *          2.操作类型type (1.浏览 2.点赞 3.评论...)
     *          大数据推荐系统: 推荐规则 唯一数字ID推荐 (sequence表里的pid,vid...)
     */
    public void sendMsg(String publishId,Integer type){
        try {
            //1.获取用户id
            Long userId = UserHolder.getUserId();
            //2.根据publishId查询动态
            Publish publish = publishApi.findById(publishId);
            //3.构建发送消息
            Map<String,String> message = new HashMap<>();
            message.put("userId",userId.toString());
            message.put("publishId",publish.getPid().toString());
            message.put("type",type.toString());
            //4.发送消息
            rocketMQTemplate.convertAndSend("tanhua-quanziR",JSON.toJSONString(message));
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("tanhua-quanziR发送失败=="+publishId+"=="+type);
        }
    }
}
